package com.ruoyi.recruit.controller;

import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.recruit.service.IERecruitService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.domain.e_recruit.EDeliver;
import com.ruoyi.recruit.service.IEDeliverService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 投递记录Controller
 * 
 * @author JYW
 * @date 2024-01-30
 */
@RestController
@RequestMapping("/recruit/deliver")
public class EDeliverController extends BaseController
{
    @Autowired
    private IEDeliverService eDeliverService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private IERecruitService recruitService;

    /**
     * 查询投递记录列表
     */
    @PreAuthorize("@ss.hasPermi('recruit:deliver:list')")
    @GetMapping("/list")
    public TableDataInfo list(EDeliver eDeliver)
    {
        startPage();
        List<EDeliver> list = eDeliverService.selectEDeliverList(eDeliver);
        List<EDeliver> updatedList = null;
        if (!list.isEmpty()){
            updatedList = list.stream()
                    .peek(eDeliver1 -> {
                        eDeliver1.setFileName(FileUtils.getName(eDeliver1.getFilePath()));
                        eDeliver1.setUsername(userService.selectUserById(eDeliver1.getuId()).getUserName());
                        eDeliver1.setDeliverName(recruitService.selectERecruitById(eDeliver1.getrId()).getpName());
                    })
                    .collect(Collectors.toList());

        }
        return getDataTable(updatedList);
    }

    /**
     * 导出投递记录列表
     */
    @PreAuthorize("@ss.hasPermi('recruit:deliver:export')")
    @Log(title = "投递记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EDeliver eDeliver)
    {
        List<EDeliver> list = eDeliverService.selectEDeliverList(eDeliver);
        ExcelUtil<EDeliver> util = new ExcelUtil<EDeliver>(EDeliver.class);
        util.exportExcel(response, list, "投递记录数据");
    }

    /**
     * 获取投递记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('recruit:deliver:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(eDeliverService.selectEDeliverById(id));
    }

    /**
     * 新增投递记录
     */
    @PreAuthorize("@ss.hasPermi('recruit:deliver:add')")
    @Log(title = "投递记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EDeliver eDeliver)
    {
        return toAjax(eDeliverService.insertEDeliver(eDeliver));
    }

    /**
     * 修改投递记录
     */
    @PreAuthorize("@ss.hasPermi('recruit:deliver:edit')")
    @Log(title = "投递记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EDeliver eDeliver)
    {
        return toAjax(eDeliverService.updateEDeliver(eDeliver));
    }

    /**
     * 删除投递记录
     */
    @PreAuthorize("@ss.hasPermi('recruit:deliver:remove')")
    @Log(title = "投递记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(eDeliverService.deleteEDeliverByIds(ids));
    }
}
